<template>
	<view class="free-to-buy">
		<!-- 顶部 -->
		<header-top />
		<!-- 顶部图片 -->
		<view class="free-top"><view class="active-gz" @click="openRules">活动规则</view></view>
		<!-- 活动列表 -->
		<view class="active-box">
			<view class="active-title">活动列表</view>
			<active-list :list="list" @activeDetail="activeDetail" />
		</view>
		<!-- 加载更多 -->
		<uni-load-more :status="status"></uni-load-more>
		<!-- 活动规则 -->
		<activity-rules-popup ref="activityRulesPopup" />
	</view>
</template>

<script>
const app = getApp();
import activeList from './components/active-list/index.vue';
import activityRulesPopup from './components/activity-rules-popup/index.vue';
import { freeCat } from '@/api/active.js';
import headerTop from '@/wxcomponents/header/index.vue'
export default {
	components: {
		activeList,
		activityRulesPopup,
		headerTop
	},
	data() {
		return {
			page: 1,
			total: 0,
			list: [],
			status: 'loading'
		};
	},
	onLoad() {
		if (app.globalData.openid) {
			this.freeCat();
		} else {
			app.getOpenId().then(res => {
				this.freeCat();
			});
		}
	},
	methods: {
		// 猫咪列表
		async freeCat() {
			this.status = 'loading';
			const res = await freeCat({
				openid: app.globalData.openid,
				page: this.page
			});
			this.total = res.total;
			this.list = this.list.concat(res.list);
			if (this.page == this.total || this.total == 0) {
				this.status = 'noMore';
			} else {
				this.status = 'more';
			}
		},
		// 跳转活动详情
		activeDetail(id) {
			uni.navigateTo({
				url: `./freeToBuyDetail?id=${id}`
			});
		},
		// 查看活动规则
		openRules() {
			this.$refs.activityRulesPopup.open();
		}
	},
	// 上拉加载
	onReachBottom() {
		if (this.page == this.total) {
			return false;
		}
		this.page += 1;
		this.freeCat();
	}
};
</script>

<style scoped lang="scss">
.free-to-buy {
	margin-bottom: 30rpx;
	.free-top {
		width: 100%;
		height: 448rpx;
		background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/active-top-img1.png) no-repeat;
		background-size: cover;
		position: relative;
		margin-bottom: 20rpx;
		.active-gz {
			position: absolute;
			top: 178rpx;
			left: 0;
			width: 124rpx;
			height: 48rpx;
			background: #ffffff;
			border-radius: 0 28rpx 28rpx 0;
			text-align: center;
			line-height: 48rpx;
			font-size: 24rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #fa8384;
		}
	}
	.active-box {
		padding: 0 32rpx;
		.active-title {
			font-size: 32rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #666666;
			margin-bottom: 24rpx;
			letter-spacing: 2rpx;
		}
	}
}
</style>
